package com.shell.provider;

import com.shell.bee.base.utils.StringUtil;
import com.shell.customer.base.service.CusRecService;
import com.shell.customer.base.service.CusTagService;
import com.shell.customer.base.service.CustomerService;
import com.shell.customer.group.service.CusGrpService;
import com.shell.customer.po.CusGrp;
import com.shell.customer.po.CusRec;
import com.shell.customer.po.CusTag;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CustomerProvider {
	@Autowired
	CusRecService cusRecService;
	@Autowired
	private CusGrpService cusGrpService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private CusTagService cusTagService;

	@RequestMapping({ "/customer/selectCusById" })
	CusRec selectCusById(String cusId) {
		return (CusRec) this.cusRecService.selectByKey(cusId);
	}

	@RequestMapping({ "/customer/getRelCusIds" })
	public Map<String, String> getRelCusIds(@RequestParam("cusId") String cusId,
			@RequestParam(value = "cusType", required = false) String cusType) {
		Map<String, String> result = new HashMap();
		CusRec cusRec = (CusRec) this.cusRecService.selectByKey(cusId);
		if ((cusRec != null) && (!StringUtil.emptyAndNull(cusRec.getGrpId()))) {
			result.put("gly_ent", cusRec.getGrpId());
		}
		return result;
	}

	@RequestMapping({ "/customer/findCusGroup" })
	public List<CusGrp> findCusGroup(@RequestParam("cusId") String cusId, @RequestParam("tagCodes") String tagCodes) {
		return this.cusGrpService.selectCusGroupByTagCode(cusId, tagCodes);
	}

	@RequestMapping({ "/customer/getCusyGroupIdAndTagCodes" })
	public List<CusRec> getCusyGroupIdAndTagCodes(@RequestParam(value = "grpId", required = false) String grpId,
			@RequestParam("tagCodes") String tagCodes) {
		return this.customerService.selectCusByGroupIdAndTagCode(grpId, tagCodes);
	}

	@RequestMapping({ "/customer/getCusTagForEnt" })
	public List<CusTag> getCusTagForEnt(@RequestParam("entId") String entId, @RequestParam("grpId") String grpId) {
		return this.cusTagService.getCusTagForEnt(entId, grpId);
	}

	public CusRec selectCusByIdType(@RequestParam("idType") String idType, @RequestParam("idCode") String idCode) {
		return this.cusRecService.selectCusByIdType(idType, idCode);
	}

	@RequestMapping({ "/customer/getCusTypeById" })
	public String getCusTypeById(@RequestParam("entId") String entId) {
		CusRec cusRec = selectCusById(entId);
		if (cusRec == null) {
			return "";
		}
		return cusRec.getCusType();
	}

	@RequestMapping({ "/customer/findCusIsOperatorById" })
	public boolean findCusIsOperatorById(@RequestParam("entId") String entId) {
		return this.cusTagService.hasTag(entId, null, "OPERATOR");
	}

	@RequestMapping({ "/customer/findCusIsArtisanById" })
	boolean findCusIsArtisanById(@RequestParam("entId") String entId) {
		return this.cusTagService.hasTag(entId, null, "ARTISAN");
	}

	@RequestMapping({ "/customer/getTagCodesById" })
	public String getTagCodesById(@RequestParam("entId") String entId,
			@RequestParam(value = "grpId", required = false) String grpId) {
		List<CusTag> cusTags = getCusTagForEnt(entId, grpId);
		Set<String> tagCodes = new HashSet();
		for (CusTag cusTag : cusTags) {
			tagCodes.add(cusTag.getTagCode());
		}
		return String.join(",", tagCodes);
	}
}
